// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.iso.areas;

import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Vector;
import zombie.core.Rand;
import zombie.inventory.ItemContainer;
import zombie.iso.IsoCell;
import zombie.iso.IsoGridSquare;
import zombie.iso.IsoObject;
import zombie.iso.IsoRoomLight;
import zombie.iso.IsoWorld;
import zombie.iso.MetaObject;
import zombie.iso.RoomDef;
import zombie.iso.objects.IsoLightSwitch;
import zombie.iso.objects.IsoWindow;
import zombie.network.GameServer;

public final class IsoRoom {
    private static final ArrayList<IsoGridSquare> tempSquares = new ArrayList();
    public final Vector<IsoGridSquare> Beds = new Vector();
    public Rectangle bounds;
    public IsoBuilding building = null;
    public final ArrayList<ItemContainer> Containers = new ArrayList();
    public final ArrayList<IsoWindow> Windows = new ArrayList();
    public final Vector<IsoRoomExit> Exits = new Vector();
    public int layer;
    public String RoomDef = "none";
    public final Vector<IsoGridSquare> TileList = new Vector();
    public int transparentWalls = 0;
    public final ArrayList<IsoLightSwitch> lightSwitches = new ArrayList();
    public final ArrayList<IsoRoomLight> roomLights = new ArrayList();
    public final ArrayList<IsoObject> WaterSources = new ArrayList();
    public int seen = 1000000000;
    public int visited = 1000000000;
    public RoomDef def;
    public final ArrayList<RoomDef.RoomRect> rects = new ArrayList(1);
    public final ArrayList<IsoGridSquare> Squares = new ArrayList();

    public IsoBuilding getBuilding() {
        return this.building;
    }

    public String getName() {
        return this.RoomDef;
    }

    public IsoBuilding CreateBuilding(IsoCell cell) {
        IsoBuilding isoBuilding = new IsoBuilding(cell);
        this.AddToBuilding(isoBuilding);
        return isoBuilding;
    }

    public boolean isInside(int x, int y, int z) {
        for (int int0 = 0; int0 < this.rects.size(); int0++) {
            int int1 = ((RoomDef.RoomRect)this.rects.get(int0)).x;
            int int2 = ((RoomDef.RoomRect)this.rects.get(int0)).y;
            int int3 = ((RoomDef.RoomRect)this.rects.get(int0)).getX2();
            int int4 = ((RoomDef.RoomRect)this.rects.get(int0)).getY2();
            if (x >= int1 && y >= int2 && x < int3 && y < int4 && z == this.layer) {
                return true;
            }
        }

        return false;
    }

    public IsoGridSquare getFreeTile() {
        boolean _boolean = false;
        IsoGridSquare isoGridSquare = null;
        int int0 = 100;

        while (!_boolean && int0 > 0) {
            int0--;
            _boolean = true;
            if (this.TileList.isEmpty()) {
                return null;
            }

            isoGridSquare = (IsoGridSquare)this.TileList.get(Rand.Next(this.TileList.size()));

            for (int int1 = 0; int1 < this.Exits.size(); int1++) {
                if (isoGridSquare.getX() == ((IsoRoomExit)this.Exits.get(int1)).x && isoGridSquare.getY() == ((IsoRoomExit)this.Exits.get(int1)).y) {
                    _boolean = false;
                }
            }

            if (_boolean && !isoGridSquare.isFree(true)) {
                _boolean = false;
            }
        }

        return int0 < 0 ? null : isoGridSquare;
    }

    void AddToBuilding(IsoBuilding isoBuilding) {
        this.building = isoBuilding;
        isoBuilding.AddRoom(this);

        for (IsoRoomExit isoRoomExit : this.Exits) {
            if (isoRoomExit.To.From != null && isoRoomExit.To.From.building == null) {
                isoRoomExit.To.From.AddToBuilding(isoBuilding);
            }
        }
    }

    public ArrayList<IsoObject> getWaterSources() {
        return this.WaterSources;
    }

    public void setWaterSources(ArrayList<IsoObject> _WaterSources) {
        this.WaterSources.clear();
        this.WaterSources.addAll(_WaterSources);
    }

    public boolean hasWater() {
        if (this.WaterSources.isEmpty()) {
            return false;
        } else {
            Iterator iterator = this.WaterSources.iterator();

            while (iterator != null && iterator.hasNext()) {
                IsoObject isoObject = (IsoObject)iterator.next();
                if (isoObject.hasWater()) {
                    return true;
                }
            }

            return false;
        }
    }

    public void useWater() {
        if (!this.WaterSources.isEmpty()) {
            Iterator iterator = this.WaterSources.iterator();

            while (iterator != null && iterator.hasNext()) {
                IsoObject isoObject = (IsoObject)iterator.next();
                if (isoObject.hasWater()) {
                    isoObject.useWater(1);
                    break;
                }
            }
        }
    }

    public ArrayList<IsoWindow> getWindows() {
        return this.Windows;
    }

    public void addSquare(IsoGridSquare sq) {
        if (!this.Squares.contains(sq)) {
            this.Squares.add(sq);
        }
    }

    public void refreshSquares() {
        this.Windows.clear();
        this.Containers.clear();
        this.WaterSources.clear();
        this.Exits.clear();
        tempSquares.clear();
        tempSquares.addAll(this.Squares);
        this.Squares.clear();

        for (int _int = 0; _int < tempSquares.size(); _int++) {
            this.addSquare((IsoGridSquare)tempSquares.get(_int));
        }
    }

    private void addExitTo(IsoGridSquare isoGridSquare0, IsoGridSquare isoGridSquare1) {
        IsoRoom isoRoom0 = null;
        IsoRoom isoRoom1 = null;
        if (isoGridSquare0 != null) {
            isoRoom0 = isoGridSquare0.getRoom();
        }

        if (isoGridSquare1 != null) {
            isoRoom1 = isoGridSquare1.getRoom();
        }

        if (isoRoom0 != null || isoRoom1 != null) {
            IsoRoom isoRoom2 = isoRoom0;
            if (isoRoom0 == null) {
                isoRoom2 = isoRoom1;
            }

            IsoRoomExit isoRoomExit0 = new IsoRoomExit(isoRoom2, isoGridSquare0.getX(), isoGridSquare0.getY(), isoGridSquare0.getZ());
            isoRoomExit0.type = IsoRoomExit.ExitType.Door;
            if (isoRoom2 == isoRoom0) {
                if (isoRoom1 != null) {
                    IsoRoomExit isoRoomExit1 = isoRoom1.getExitAt(isoGridSquare1.getX(), isoGridSquare1.getY(), isoGridSquare1.getZ());
                    if (isoRoomExit1 == null) {
                        isoRoomExit1 = new IsoRoomExit(isoRoom1, isoGridSquare1.getX(), isoGridSquare1.getY(), isoGridSquare1.getZ());
                        isoRoom1.Exits.add(isoRoomExit1);
                    }

                    isoRoomExit0.To = isoRoomExit1;
                } else {
                    isoRoom0.building.Exits.add(isoRoomExit0);
                    if (isoGridSquare1 != null) {
                        isoRoomExit0.To = new IsoRoomExit(isoRoomExit0, isoGridSquare1.getX(), isoGridSquare1.getY(), isoGridSquare1.getZ());
                    }
                }

                isoRoom0.Exits.add(isoRoomExit0);
            } else {
                isoRoom1.building.Exits.add(isoRoomExit0);
                if (isoGridSquare1 != null) {
                    isoRoomExit0.To = new IsoRoomExit(isoRoomExit0, isoGridSquare1.getX(), isoGridSquare1.getY(), isoGridSquare1.getZ());
                }

                isoRoom1.Exits.add(isoRoomExit0);
            }
        }
    }

    private IsoRoomExit getExitAt(int int3, int int2, int int1) {
        for (int int0 = 0; int0 < this.Exits.size(); int0++) {
            IsoRoomExit isoRoomExit = (IsoRoomExit)this.Exits.get(int0);
            if (isoRoomExit.x == int3 && isoRoomExit.y == int2 && isoRoomExit.layer == int1) {
                return isoRoomExit;
            }
        }

        return null;
    }

    public void removeSquare(IsoGridSquare sq) {
        this.Squares.remove(sq);
        IsoRoomExit isoRoomExit = this.getExitAt(sq.getX(), sq.getY(), sq.getZ());
        if (isoRoomExit != null) {
            this.Exits.remove(isoRoomExit);
            if (isoRoomExit.To != null) {
                isoRoomExit.From = null;
            }

            if (this.building.Exits.contains(isoRoomExit)) {
                this.building.Exits.remove(isoRoomExit);
            }
        }

        for (int _int = 0; _int < sq.getObjects().size(); _int++) {
            IsoObject isoObject = sq.getObjects().get(_int);
            if (isoObject instanceof IsoLightSwitch) {
                this.lightSwitches.remove(isoObject);
            }
        }
    }

    public void spawnZombies() {
        zombie.VirtualZombieManager.instance.addZombiesToMap(1, this.def, false);
    }

    public void onSee() {
        for (int _int = 0; _int < this.getBuilding().Rooms.size(); _int++) {
            IsoRoom isoRoom1 = (IsoRoom)this.getBuilding().Rooms.elementAt(_int);
            if (isoRoom1 != null && !isoRoom1.def.bExplored) {
                isoRoom1.def.bExplored = true;
            }

            IsoWorld.instance.getCell().roomSpotted(isoRoom1);
        }
    }

    public Vector<IsoGridSquare> getTileList() {
        return this.TileList;
    }

    public ArrayList<IsoGridSquare> getSquares() {
        return this.Squares;
    }

    public ArrayList<ItemContainer> getContainer() {
        return this.Containers;
    }

    public IsoGridSquare getRandomSquare() {
        return this.Squares.isEmpty() ? null : (IsoGridSquare)this.Squares.get(Rand.Next(this.Squares.size()));
    }

    public IsoGridSquare getRandomFreeSquare() {
        int _int = 100;
        IsoGridSquare isoGridSquare = null;
        if (GameServer.bServer) {
            while (_int > 0) {
                isoGridSquare = IsoWorld.instance
                    .CurrentCell
                    .getGridSquare(this.def.getX() + Rand.Next(this.def.getW()), this.def.getY() + Rand.Next(this.def.getH()), this.def.level);
                if (isoGridSquare != null && isoGridSquare.getRoom() == this && isoGridSquare.isFree(true)) {
                    return isoGridSquare;
                }

                _int--;
            }

            return null;
        } else if (this.Squares.isEmpty()) {
            return null;
        } else {
            while (_int > 0) {
                isoGridSquare = (IsoGridSquare)this.Squares.get(Rand.Next(this.Squares.size()));
                if (isoGridSquare.isFree(true)) {
                    return isoGridSquare;
                }

                _int--;
            }

            return null;
        }
    }

    public boolean hasLightSwitches() {
        if (!this.lightSwitches.isEmpty()) {
            return true;
        } else {
            for (int _int = 0; _int < this.def.objects.size(); _int++) {
                if (((MetaObject)this.def.objects.get(_int)).getType() == 7) {
                    return true;
                }
            }

            return false;
        }
    }

    public void createLights(boolean active) {
        if (this.roomLights.isEmpty()) {
            for (int _int = 0; _int < this.def.rects.size(); _int++) {
                RoomDef.RoomRect roomRect = (RoomDef.RoomRect)this.def.rects.get(_int);
                IsoRoomLight isoRoomLight = new IsoRoomLight(this, roomRect.x, roomRect.y, this.def.level, roomRect.w, roomRect.h);
                this.roomLights.add(isoRoomLight);
            }
        }
    }

    public RoomDef getRoomDef() {
        return this.def;
    }

    public ArrayList<IsoLightSwitch> getLightSwitches() {
        return this.lightSwitches;
    }
}
